// Learn TypeScript:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/life-cycle-callbacks.html

import AudioManager from "./Audio";
import FadePanel from "./FadePanel";
import GameData from "./GameData";
import GameScene from "./GameScene";
import SceneLoad from "./SceneLoad";
import BannerAd from "./Zsrc/BannerAd";
import TreasureBox from "./Zsrc/TreasureBox";

const { ccclass, property } = cc._decorator;

@ccclass
export default class GameUI extends cc.Component {

    public static get Instance(): GameUI {
        return this.instance;
    }

    private static instance: GameUI;


    public set Coins(value: number) {
        this.coins = value;
        this.coinLabel.string = this.coins.toString();
    }
    public get Coins(): number {
        return this.coins;
    }

    @property(cc.Node)
    finishPanel: cc.Node = null;

    @property(cc.Node)
    pausePanel: cc.Node = null;

    @property(cc.Label)
    coinLabel: cc.Label = null;

    @property(cc.Label)
    finishCoin: cc.Label = null;

    @property(cc.Label)
    allCoin: cc.Label = null;

    @property(cc.Label)
    finishGuest: cc.Label = null;

    coins = 0;

    // LIFE-CYCLE CALLBACKS:

    onLoad() {
        GameUI.instance = this;
    }

    start() {
        this.scheduleOnce(() => {
            this.pausePanel.active = false;
        });
    }

    // update (dt) {}

    Finish() {
        var datas = GameData.Instance.Datas;
        datas[0] += this.coins;
        datas[1]++;
        datas[3] += 30;
        GameData.Instance.Datas = datas;
        this.finishPanel.active = true;
        this.finishCoin.string = this.coins.toString();
        this.allCoin.string = datas[0].toString();
        this.finishGuest.string = GameScene.Instance.guestNum.toString();
        if (BannerAd.Instance.cityIswhite) TreasureBox.Show(null);
        else BannerAd.Instance.ShowCustomAd();
    }

    Back() {
        FadePanel.Instance.FadeIn(() => {
            SceneLoad.GoScene("MainScene");
        });
        AudioManager.Instance.ButtonAudio();
    }

    Reset() {
        FadePanel.Instance.FadeIn(() => {
            SceneLoad.GoScene("GameScene");
        });
        AudioManager.Instance.ButtonAudio();
    }

    Pause() {
        this.pausePanel.active = true;
        if (BannerAd.Instance.cityIswhite) BannerAd.Instance.ShowCustomAd(null, 0);
        cc.director.pause();
    }

    Continue() {
        cc.director.resume();
        this.pausePanel.active = false;
    }

    Upgrade() {
        FadePanel.Instance.FadeIn(() => {
            SceneLoad.GoScene("UpgradeScene");
        });
        AudioManager.Instance.ButtonAudio();
    }
}
